<template>
  <ych-modal
    :fullscreen="true"
    :title="title"
    :visible="visible"
    @ok="handleOk"
    @cancel="close"
    :footer="disableSubmit ? null : footer"
    :cancelText="$t('m.common.button.close')">

    <template slot="footer">
      <a-button key="back" @click="close">{{ $t('m.common.button.close') }}</a-button>
      <a-button key="submit" type="primary" @click="handleOk">{{ $t('m.common.button.ok') }}</a-button>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-row class="grid-row">
            <a-col v-bind="colLayout2.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esTitle')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
                  <a-input
                          :disabled="disabledRules.esTitle"
                          placeholder=""
                          type="text"
                  v-decorator="['esTitle', validatorRules.esTitle]"
                  />
                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esType')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
                  <a-radio-group v-decorator="['esType',validatorRules.esType]" button-style="solid">
                    <a-radio-button value="2">{{$t('m.system.message.other.esType_option2')}}</a-radio-button>
                    <a-radio-button value="1">{{$t('m.system.message.other.esType_option1')}}</a-radio-button>
                    <a-radio-button v-if="showDingtalk" value="5">{{$t('m.system.message.other.esType_option5')}}</a-radio-button>
                  </a-radio-group>

                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esReceiver')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
                  <a-input
                          :disabled="disabledRules.esReceiver"
                          placeholder=""
                          type="text"
                  v-decorator="['esReceiver', validatorRules.esReceiver]"
                  />
                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esTemplateCode')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
                  <a-input
                          :disabled="disabledRules.esTemplateCode"
                          placeholder=""
                          type="text"
                  v-decorator="['esTemplateCode', validatorRules.esTemplateCode]"
                  />
                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.fixed.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esParam')" :label-col="colLayout2.fixed.labelCol" :wrapper-col="colLayout2.fixed.wrapperCol">
                  <a-input
                          :disabled="disabledRules.esParam"
                          :placeholder="$t('m.system.message.verify.esParam')"
                          type="text"
                  v-decorator="['esParam', validatorRules.esParam]"
                  />
                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.fixed.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esContent')" :label-col="colLayout2.fixed.labelCol" :wrapper-col="colLayout2.fixed.wrapperCol">
                  <a-textarea
                          :autoSize="{
                                  minRows: 3,
                                  maxRows: 5
                                  }"
                          :disabled="disabledRules.esContent"
                          :rows="4"
                  v-decorator="['esContent', validatorRules.esContent]"
                  />
                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esSendTime')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
                  <ych-date
                          v-decorator="['esSendTime', validatorRules.esSendTime]"
                          style="width: 100%"
                          :disabled="disabledRules.esSendTime"
                          showTime
                          dateFormat="YYYY-MM-DD HH:mm:ss"></ych-date>
                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esSendStatus')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
                  <!--bledRules.esSendStatus"
                  v-decorator="['esSendStatus', validatorRules.esSendStatus,{'initialValue':'0'}]"
                              :type="'radio'"
                              :triggerChange="true" dictCode="sys_msg_send_status"/>-->

                   <a-radio-group v-decorator="['esSendStatus',validatorRules.esSendStatus]" button-style="solid">
                     <a-radio-button value="0">{{$t('m.system.message.other.status_option0')}}</a-radio-button>
                     <a-radio-button value="1">{{$t('m.system.message.other.status_option1')}}</a-radio-button>
                     <a-radio-button value="2">{{$t('m.system.message.other.status_option2')}}</a-radio-button>
                   </a-radio-group>

                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esSendNum')" :label-col="colLayout2.labelCol" :wrapper-col="colLayout2.wrapperCol">
                    <a-input-number
                            style="width: 100%"
                            :min="0"
                            :max="999999999"
                            :disabled="disabledRules.esSendNum"
                            :step="1"
                            placeholder=""
                    v-decorator="['esSendNum', validatorRules.esSendNum]"
                    />
                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.fixed.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.esResult')" :label-col="colLayout2.fixed.labelCol" :wrapper-col="colLayout2.fixed.wrapperCol">
              <a-textarea
                          :autoSize="{
                                  minRows: 2,
                                  maxRows: 4
                                  }"
                          :disabled="disabledRules.esResult"
                          :rows="4"
                  v-decorator="['esResult', validatorRules.esResult]"
                  />
                </a-form-item>
            </a-col>
            <a-col v-bind="colLayout2.fixed.cols" class="grid-col">
                <a-form-item :label="$t('m.system.message.entity.remark')" :label-col="colLayout2.fixed.labelCol" :wrapper-col="colLayout2.fixed.wrapperCol">
                <a-textarea
                          :autoSize="{
                                  minRows: 2,
                                  maxRows: 4
                                  }"
                          :disabled="disabledRules.remark"
                          :rows="4"
                  v-decorator="['remark', validatorRules.remark]"
                  />
                </a-form-item>
            </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </ych-modal>
</template>

<script>

  import {httpAction} from '@/api/manage'
  import moment from "moment"
  import YchConstant from "@/api/YchConstant";
  import {GardLayoutMixin} from '@/mixins/GardLayoutMixin'
  import {YchDate} from '@yuncheng/ych-components'
  import config from '@/settings'

  export default {
    name: "SysMessageModal",
    mixins: [GardLayoutMixin],
    components: {
      YchDate
    },
    data() {
      return {
        showDingtalk: config.showDingtalk,
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        disableSubmit: true,
        url: {
          add: YchConstant.PREFIX_SYSTEM + "/message/sysMessage/add",
          edit: YchConstant.PREFIX_SYSTEM + "/message/sysMessage/edit",
        },
        validatorRules: {
            esTitle: {
                rules: [{"required":true,"message":this.$t('m.common.tip.verify_required')}]
            },
            esType: {
                rules: [{"required":true,"message":this.$t('m.common.tip.verify_required')}]
            },
            esReceiver: {
                rules: [{"required":true,"message":this.$t('m.common.tip.verify_required')}]
            },
            esTemplateCode: {
                rules: []
            },
            esParam: {
                rules: []
            },
            esContent: {
                rules: [{"required":true,"message":this.$t('m.common.tip.verify_required')}]
            },
            esSendTime: {
                rules: []
            },
            esSendStatus: {
                rules: []
            },
            esSendNum: {
                rules: []
            },
            esResult: {
                rules: []
            },
            remark: {
                rules: []
            },
         },
         disabledRules: {
            esTitle: false,
            esType: false,
            esReceiver: false,
            esTemplateCode: false,
            esParam: false,
            esContent: false,
            esSendTime: false,
            esSendStatus: false,
            esSendNum: false,
            esResult: false,
            remark: false,
         },
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({
          esType:'2',
          esSendStatus:'0'
        });
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(this.lodash.pick(this.model, 'esContent', 'esParam', 'esReceiver', 'esResult', 'esSendNum', 'esSendStatus', 'esTitle', 'esType', 'remark','esSendTime'))
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            //formData.esSendTime = formData.esSendTime ? formData.esSendTime.format('YYYY-MM-DD HH:mm:ss') : null;

            //console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.messageUtil.success(res.message);
                that.$emit('ok');
              } else {
                that.messageUtil.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })


          }
        })
      },
      handleCancel() {
        this.close()
      },


    }
  }
</script>
